package com.yic.module.wms.controller.admin.receiveapply;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.wms.controller.admin.receiveapply.vo.*;
import com.yic.module.wms.dal.dataobject.receiveapply.ReceiveApplyDO;
import com.yic.module.wms.convert.receiveapply.ReceiveApplyConvert;
import com.yic.module.wms.service.receiveapply.ReceiveApplyService;

@Tag(name = "管理后台 - 领料申请单")
@RestController
@RequestMapping("/wms/receive-apply")
@Validated
public class ReceiveApplyController {

    @Resource
    private ReceiveApplyService receiveApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建领料申请单")
    @PreAuthorize("@ss.hasPermission('wms:receive-apply:create')")
    public CommonResult<Long> createReceiveApply(@Valid @RequestBody ReceiveApplyCreateReqVO createReqVO) {
        return success(receiveApplyService.createReceiveApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新领料申请单")
    @PreAuthorize("@ss.hasPermission('wms:receive-apply:update')")
    public CommonResult<Boolean> updateReceiveApply(@Valid @RequestBody ReceiveApplyUpdateReqVO updateReqVO) {
        receiveApplyService.updateReceiveApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除领料申请单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:receive-apply:delete')")
    public CommonResult<Boolean> deleteReceiveApply(@RequestParam("id") Long id) {
        receiveApplyService.deleteReceiveApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得领料申请单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:receive-apply:query')")
    public CommonResult<ReceiveApplyRespVO> getReceiveApply(@RequestParam("id") Long id) {
        ReceiveApplyDO receiveApply = receiveApplyService.getReceiveApply(id);
        return success(ReceiveApplyConvert.INSTANCE.convert(receiveApply));
    }

    @GetMapping("/list")
    @Operation(summary = "获得领料申请单列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('wms:receive-apply:query')")
    public CommonResult<List<ReceiveApplyRespVO>> getReceiveApplyList(@RequestParam("ids") Collection<Long> ids) {
        List<ReceiveApplyDO> list = receiveApplyService.getReceiveApplyList(ids);
        return success(ReceiveApplyConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得领料申请单分页")
    @PreAuthorize("@ss.hasPermission('wms:receive-apply:query')")
    public CommonResult<PageResult<ReceiveApplyRespVO>> getReceiveApplyPage(@Valid ReceiveApplyPageReqVO pageVO) {
        PageResult<ReceiveApplyDO> pageResult = receiveApplyService.getReceiveApplyPage(pageVO);
        return success(ReceiveApplyConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出领料申请单 Excel")
    @PreAuthorize("@ss.hasPermission('wms:receive-apply:export')")
    @OperateLog(type = EXPORT)
    public void exportReceiveApplyExcel(@Valid ReceiveApplyExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ReceiveApplyDO> list = receiveApplyService.getReceiveApplyList(exportReqVO);
        // 导出 Excel
        List<ReceiveApplyExcelVO> datas = ReceiveApplyConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "领料申请单.xls", "数据", ReceiveApplyExcelVO.class, datas);
    }

}
